package com.zhu.day.factory;

import java.util.Objects;

public class PromotionFactory {

    public static DoPromotion getDoPromotionFactory(String type) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
         /*这里通过反射的方式获取到水果子类的字节码，即类对象，通过类对象的newInstance()方法创建水果子类
        实例*/
        try {
            Class fruit = Class.forName(PromotionFactory.class.getPackage().getName()+"."+type);
            if (Objects.isNull(fruit)){
                return null;
            }
            return (DoPromotion) fruit.newInstance();
        } catch (ClassNotFoundException e) {
            return null;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }



}
